package member

import (
	"net/url"

	"path/filepath"

	"net/http"

	"gopkg.in/mgo.v2"
)

func Get(u *url.URL, w http.ResponseWriter, db *mgo.Database) {
	switch u.Query().Get("t") {
	case "auth":
		auth(filepath.Ext(u.Path), u.Query().Get("name"), u.Query().Get("password"), u.Query().Get("school"), w, db)
	case "t":
		teacher(filepath.Ext(u.Path), u.Query().Get("school"), w, db)
	case "s":
		student(filepath.Ext(u.Path), u.Query().Get("id"), w, db)
	case "q":
		query(filepath.Ext(u.Path), u.Query().Get("id"), w, db)
	case "m1":
		recover(filepath.Ext(u.Path), u.Query().Get("mail"), w, db)
	case "m2":
		recover2(filepath.Ext(u.Path), u.Query().Get("code"), u.Query().Get("token"), w, db)
	case "", "a":
		fallthrough
	default:
		all(filepath.Ext(u.Path), w, db)
	}
}

func Post(u *url.URL, w http.ResponseWriter, body []byte, db *mgo.Database) {
	create(filepath.Ext(u.Path), body, w, db)
}

func Put(u *url.URL, w http.ResponseWriter, body []byte, db *mgo.Database) {
	switch u.Query().Get("t") {
	case "r":
		modifyPassword(filepath.Ext(u.Path), u.Query().Get("code"), u.Query().Get("mail"), body, w, db)
	case "p":
		modifyDPassword(filepath.Ext(u.Path), u.Query().Get("id"), u.Query().Get("token"), body, w, db)
	case "n":
		modifyName(filepath.Ext(u.Path), u.Query().Get("id"), u.Query().Get("token"), body, w, db)
	case "m":
		modifyMail(filepath.Ext(u.Path), u.Query().Get("id"), u.Query().Get("token"), body, w, db)
	case "", "i":
		fallthrough
	default:
		edit(filepath.Ext(u.Path), u.Query().Get("id"), body, w, db)
	}
}

func Del(u *url.URL, w http.ResponseWriter, db *mgo.Database) {
	switch u.Query().Get("t") {
	case "", "i":
		fallthrough
	default:
		remove(filepath.Ext(u.Path), u.Query().Get("id"), w, db)
	}

}
